require("../../utils/util.js");

var a = getApp();

Page({
    data: {
        userId: "",
        baseUrl: a.globalData.baseUrl,
        page: 1,
        limit: 20,
        hasMore: !0,
        orderList: [],
        good: [],
        classname: ""
    },
    onLoad: function(t) {
        var e = this;
        if (e.data.hasMore) {
            e.setData({
                nickName: a.globalData.nickName,
                avatarUrl: a.globalData.avatarUrl,
                userId: wx.getStorageSync("userId")
            });
            var r = e.data.baseUrl + "m/order/goodsOrderList";
            this.getinitData(r);
        }
    },
    getinitData: function(a) {
        var t = this;
        wx.request({
            url: a,
            data: {
                userId: t.data.userId,
                page: t.data.page,
                limit: t.data.limit
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                if (0 != e.code) wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }); else {
                    var r = t.data.page, o = t.data.orderList;
                    e.orderList.length < t.data.limit ? t.setData({
                        orderList: o.concat(e.orderList),
                        hasMore: !1,
                        page: r + 1
                    }) : t.setData({
                        orderList: o.concat(e.orderList),
                        hasMore: !0,
                        page: r + 1
                    });
                }
            }
        });
    },
    cancelOrder: function(t) {
        var e = this, r = a.globalData.baseUrl + a.globalData.urlCancleOrder;
        wx.request({
            url: r,
            data: {
                orderId: t.currentTarget.dataset.orderid,
                state: t.currentTarget.dataset.state
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var r = JSON.parse(t.data);
                if (console.log(r), 0 != r.code) wx.showToast({
                    title: r.msg,
                    icon: "none",
                    duration: 1500
                }); else {
                    e.setData({
                        page: 1,
                        limit: 20,
                        orderList: []
                    });
                    var o = e.data.baseUrl + a.globalData.urlGoodsOrderList;
                    e.getinitData(o);
                }
            }
        });
    },
    getPayData: function(t) {
        var e = this, r = a.globalData.urlBase + a.globalData.urlPayData;
        wx.request({
            url: r,
            data: {
                orderId: t,
                userId: e.data.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 2e3
                }) : e.wxPay(t);
            }
        });
    },
    wxPay: function(t) {
        wx.requestPayment({
            timeStamp: t.timeStamp,
            nonceStr: t.nonceStr,
            package: "prepay_id=" + t.totalId,
            signType: "MD5",
            paySign: t.sign,
            success: function(a) {
                that.setData({
                    page: 1,
                    limit: 20,
                    orderList: []
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                    duration: 500
                });
            },
            complete: function(t) {
                var e = that.data.baseUrl + a.globalData.urlGoodsOrderList;
                that.getinitData(e);
            }
        });
    },
    toPay: function(a) {
        this.getPayData(a.currentTarget.dataset.orderid);
    },
    onShareAppMessage: function() {}
});